﻿using System;
using System.Collections.Generic;
using System.Linq;
using Gp.Scripts.Core;
using Sirenix.OdinInspector;
using Sirenix.Utilities;
using UnityEngine;

namespace Gp.Scripts.Database {
    [Serializable]
    public class ClipItem {
        [TableColumnWidth(40, true)]
        public string clipNameInTrigger;
        [TableColumnWidth(40, true)]
        public AnimationClip clipAsset;
    }
    
    [GlobalConfig("Assets/AUIP/Config")]
    public class AnimationClipDatabase: GlobalConfig<AnimationClipDatabase> {
        [TableList(IsReadOnly = true, ShowPaging = true), ShowInInspector] [SerializeField]
        private List<ClipItem> clipItems;
        public List<ClipItem> ClipItems => clipItems;
        [TableList(IsReadOnly = true, ShowPaging = true), ShowInInspector] [SerializeField]
        public List<ClipItem> addList = new List<ClipItem>();
        public new static AnimationClipDatabase Instance =>
# if UNITY_EDITOR
            GlobalConfig<AnimationClipDatabase>.Instance;
# else
            SystemDatabaseLoader.Instance.GetConfig<AnimationClipDatabase>();
# endif
        
        
# if UNITY_EDITOR
        [Button("添加动画片段", ButtonSizes.Medium)]
        private void AddItem() {
            addList.Add(new ClipItem());
        }

        [Button("确认添加", ButtonSizes.Medium)]
        private void ConfirmAdd() {
            clipItems = clipItems.Union(addList).ToList();
            addList.Clear();
        }
# endif

        public AnimationClip FindClipByName(string str) {
            return clipItems.Where(x => x.clipNameInTrigger.Equals(str))
                .Select(x => x.clipAsset)
                .FirstOrDefault();
        }
    }
}